/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","fu/app/comm/comm",
    "scprit/followup/app/datatable/datatable.servers.js",
    "scprit/followup/app/datatable/datatable.ui.js",
    "scprit/followup/app/lostFollowupManager/lostFollowupRecord.ui.js"],
    function($,$bb,$comm,$datatableServers,$datatableUi,$lostFollowupRecordUi){
	var servers = {};
	var URL = $bb.baseUrl("followup");
    var sessinonUserName = JSON.parse($bb.getSessionStorage("account")).alias;
    /**
     * 加载队列
     */
    servers.ajaxQueues = function(Callback){
        $bb.ajax({
            url: URL + "followup/service/queue/followQueueListQuery",
            success: function (data) {
                if(data.success){
                    Callback(data.data.rows);
                }
            }
        });
    }
    /**
     * 失访原因
     */
    var ajaxLostReason = function(callback,className,data1){
        $bb.ajax({
            url: URL + "followup/service/patient/lost/followLostReasonQuery",
            success: function (data) {
                if(data.success){
                    callback(data.data.rows,className,data1);
                }
            }
        });
    }
    /**
     * 请求失访原因成功后的回调函数
     */
    var ajaxLostReasonCallBack = function(rows,className,data){
        $lostFollowupRecordUi.reasonInit({ 
            "list": rows,
            "dom": $("." + className + " .modal-body"),
        });
        $("." + className).modal('show');
        $(".lostfollowupreason-group .item").unbind("click");
        $(".lostfollowupreason-group .item").click(function(){
            var reasonId = $(this).attr("id");
            var params = {
                "lostInfo": {
                    "followQueueId": data.followQueueId,
                    "patientId": data.patientId,
                    "ptLostId": data.ptLostId,
                    "reasonId": reasonId
                },
                "oprInfo": {
                    "opCode": "1013",
                    "opNote": "【"+sessinonUserName +"】医生修改了【" + data.patientName + "】患者的失访原因",
                    "opType": "2"
                }
            };
            lostfollowupreasonRecord(params,$datatableUi.table,className);
        });
    }
    var lostfollowupreasonRecord = function(params,table,className){
        $bb.ajax({
            url: URL + "followup/service/patient/lost/patientLostInfoModify",
            data: JSON.stringify({
                data: params        
            }),
            success: function (data) {
                if(data.success){
                    $("." + className).modal('hide');
                    // 出发dt的重新加载数据的方法
                    table.api().draw(false);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "标记失访原因出错！");
                }
            }
        });
    };
    /**
     * 入参
     */
    var inParams = function(args){
        var ageFrom, ageTo;
        if($("#ageFrom-year").val() !="" || $("#ageFrom-month").val() !=""){
            ageFrom = parseInt($("#ageFrom-year").val()*12) + parseInt($("#ageFrom-month").val()*1);
        }else{
            ageFrom = "";
        }
        if($("#ageTo-year").val() !="" || $("#ageTo-month").val() !=""){
            ageTo = parseInt($("#ageTo-year").val()*12) + parseInt($("#ageTo-month").val()*1);
        }else{
            ageTo = "";
        }
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "flag": 2,
                "patientName": $.trim($("#patientName").val()),
                "phoneNum": $.trim($("#phoneNum").val()),
                "queueId": $("#queue").val(),
                "hisNo": $.trim($("#hisNo").val()),
                "ageFrom": ageFrom,
                "ageTo": ageTo,
                "qryStartTime": $("#timeFrom").val(),
                "qryEndTime": $("#timeTo").val(),
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsHisNo = function(e){
        return e.hisNo1 ? e.hisNo1 : e.hisNo2 ? e.hisNo2 : e.hisNo3 ? e.hisNo3 : "-";
    }
    var columnsPatientName = function(e){
        var imgSrc = 'page/followup/imgs/comm/';
        imgSrc += e.gender == 'F' ? 'girl.png' : 'boy.png';
        var h = '<img class="pic-sex" src='+imgSrc+'>';
            h += '<span class="patient-name">' + e.patientName + '</span>'; 
        return h;
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":columnsHisNo},
            {"data":columnsPatientName},
            {"data":'lostDate'},
            {"data":'taskName'},
            {"data":'phoneNum'},
            {
                "data":'birthday',
                "className":"birthday"
            },
            {
                "data":'age',
                "className":"age"
            },
            {
                "data":'pregnancyWeek',
                "className":"pregnancyWeek"
            },
            {"data":'followQueueName'},
            {
                "data": null,
                "className":"lostFollowupProgress"
            },
            {
                "data": null,
                "className":"lostReason"
            }
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        // 进度
        var h1 = '';
        var className,status;
        for (var i = 0; i < data.taskStatusArr.length; i++) {
            status = data.taskStatusArr[i];
            className = status =="1" ? "finish" : status =="2" ? "overdue" : status =="0" ? "notstart" : "";
            h1 += '<i class="progress-'+className+'"></i>';
        }
        var html = '<div class="progress-wrap">'+ h1 +'</div>';
        $('td.lostFollowupProgress', row).html(html);
        var Iwidth = parseInt(100 / $("td.lostFollowupProgress .progress-wrap i", row).length) + '%';
        $("td.lostFollowupProgress .progress-wrap i", row).css({"width": Iwidth});

        // 失访原因
        var h = data.reasonName == "" ? '<div class="btn-look">登记</div>' : data.reasonName;
        $('td.lostReason', row).html(h);
        $("td.lostReason .btn-look",row).unbind("click");
        $("td.lostReason .btn-look",row).click(function(){
            var className = "lostfollowupModal";
            $comm.modal({
                "className": className,
                "tit": "失访原因",
            },function(){
                ajaxLostReason(ajaxLostReasonCallBack,className,data);
            });
        });
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        $("#lostfollowup").prev("div.row").remove();
        $("#serchBtn").click(function(){
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数 
        });
        $("#resetBtn").click(function(){
            $("#patientName,#phoneNum,#queue,#hisNo,#timeFrom,#timeTo,#ageFrom-year,#ageFrom-month,#ageTo-year,#ageTo-month").val("");
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
        });
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "followup/service/patient/lost/patientLostListQuery?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

	return servers;
});